﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ProtoBuf;

namespace Fx.Serialization
{
    public class ProtoSerializer
    {
        public T Deserialize<T>(string fullFilePath) where T : class
        {
            try
            {
                using (var fs = new FileStream(fullFilePath, FileMode.Open, FileAccess.Read, FileShare.None))
                {
                    var cache = Serializer.Deserialize<T>(fs);
                    return cache as T;
                }
            }
            catch (Exception ex)
            {
                //Logger.LogError(ex);
            }
            return null;
        }

        public void Serialize<T>(string fullFilePath, T graph)
        {
            try
            {
                using (var fs = new MemoryStream())
                {
                    Serializer.Serialize(fs, graph);
                    fs.Flush();
                    fs.Close();
                }
            }
            catch (Exception ex)
            {
                //Logger.LogError(ex);
            }
        }
    }
}
